<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script type="text/javascript">
    var productDataGrid;

    $(function() {
        productDataGrid = $('#productDataGrid').datagrid({
            url : '${ctxPath}/product/dataGrid',
            fit : true,
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'create_time',
	        sortOrder : 'asc',
            pageSize : 20,
            pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400],
            columns : [ [ {
                width : '80',
                title : '编号',
                field : 'id',
                sortable : true
            }, {
                width : '80',
                title : '产品名称',
                field : 'product_name',
                sortable : true
            },{
                width : '80',
                title : '型号',
                field : 'model',
                sortable : true
            },{
                width : '80',
                title : '单位',
                field : 'unit'
            },{
                width : '80',
                title : '价格',
                field : 'price',
                sortable : true
            },  {
                width : '80',
                title : '库存',
                field : 'store',
                sortable : true
               
            }, {
                width : '200',
                title : '备注',
                field : 'remark',
                sortable : true
            },{
                width : '130',
                title : '创建时间',
                field : 'create_time',
                sortable : true
            }, {
                field : 'action',
                title : '操作',
                width : 130,
                formatter : function(value, row, index) {
                    var str = '';
                     	<shiro:hasPermission name="/product/edit">
                            str += $.formatString('<a href="javascript:void(0)" class="product-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'glyphicon-pencil icon-blue\'" onclick="editProductFun(\'{0}\');" >编辑</a>', row.id);
                       	</shiro:hasPermission>
                      	<shiro:hasPermission name="/product/delete">
                            str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                            str += $.formatString('<a href="javascript:void(0)" class="product-easyui-linkbutton-del" data-options="plain:true,iconCls:\'glyphicon-trash icon-red\'" onclick="deleteProductFun(\'{0}\');" >删除</a>', row.id);
                      	</shiro:hasPermission>
                    return str;
                }
            }] ],
            onLoadSuccess:function(data){
                $('.product-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.product-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#productToolbar'
        });
    });
    
    function addProductFun() {
        parent.$.modalDialog({
            title : '添加',
            width : 500,
            height : 300,
            href : '${path }/product/addPage',
            buttons : [ {
                text : '添加',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = productDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#userAddForm');
                    f.submit();
                }
            } ]
        });
    }
    
    function deleteProductFun(id) {
        if (id == undefined) {//点击右键菜单才会触发这个
            var rows = productDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {//点击操作里面的删除图标会触发这个
            productDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除当前产品？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path }/product/delete', {
                    id : id
                }, function(result) {
                    result = $.parseJSON(result);
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        productDataGrid.datagrid('reload');
                    } else {
                        parent.$.messager.alert('错误', result.msg, 'error');
                    }
                    progressClose();
                }, 'text');
            }
        });
    }
    
    function editProductFun(id) {
        if (id == undefined) {
            var rows = productDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {
            productDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.modalDialog({
            title : '编辑',
            width : 500,
            height : 300,
            href : '${path }/product/editPage?id=' + id,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = productDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#userEditForm');
                    f.submit();
                }
            } ]
        });
    }
    
    function searchProductFun() {
        productDataGrid.datagrid('load', $.serializeObject($('#searchProductForm')));
    }
    function cleanProductFun() {
        $('#searchProductForm input').val('');
        productDataGrid.datagrid('load', {});
    }
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'north',border:false" style="height: 60px; overflow: hidden;background-color: #fff">
        <form id="searchProductForm">
             <table>
                <tr>
                    <th>产品名称:</th>
                    <td><input name="productName" placeholder="请输入产品名称"/></td>
                     <th></th>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                     <th>型号:</th>
                    <td><input name="model" placeholder="请输入型号"/></td>
                     <th></th>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                     <th>价格:</th>
                    <td><input name="price" placeholder="请输入价格"/></td>
                     <th></th>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <th>库存:</th>
                    <td><input name="store" placeholder="请输入库存"/></td>&nbsp;&nbsp;
                     <th></th>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td>
                        <input class="Wdate" name="createdateStart" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />至
                        <input class="Wdate" name="createdateEnd" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />
                        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'glyphicon-search',plain:true" onclick="searchProductFun();">查询</a>
                        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'glyphicon-remove-circle',plain:true" onclick="cleanProductFun();">清空</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',border:true,title:'产品列表'" >
        <table id="productDataGrid" data-options="fit:true,border:false"></table>
    </div>
</div>
<div id="productToolbar" style="display: none;">
    <shiro:hasPermission name="/product/add">
        <a onclick="addProductFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'glyphicon-plus icon-green'">添加</a>
    </shiro:hasPermission>
</div>